package com.wkhyapp.lm.weigit.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.wkhyapp.lm.R;

/**
 * Created by fang on 2021/11/2.
 */

public class XieyiDialog {
    private Dialog mDialog;
    private Context mContext;
    private View dialog_view;
    public XieyiDialog.callBack  mCallBack;
    private TextView submit_but,clear_but,xieyi_tv;
    private String desc =" 在您使用微客货源前,请您无比审慎阅读,充分理解“用户协议”和”隐私政策“各条款,包括但不限于:为了向您提供即时通讯，内容分享等服务，我们需要您的手机设备信息，操作日志等个人信息。拧可以在“设置”中查看，变更，删除个人信息并管理您的授权,您可以阅读《用户协议》和《隐私政策》了解详细信息。如您同意，请点击同意并接受我们的服务";


    public XieyiDialog(Context context) {
        this.mContext = context;
        init();
    }

    public void  init(){
        mDialog = new Dialog(mContext, R.style.custom_dialog);
        dialog_view = LayoutInflater.from(mContext).inflate(R.layout.dialog_xieyi, null);
        submit_but = (TextView)dialog_view.findViewById(R.id.submit_but);
        clear_but = (TextView)dialog_view.findViewById(R.id.clear_but);
        xieyi_tv = (TextView)dialog_view.findViewById(R.id.xieyi_tv);
        mDialog.setContentView(dialog_view);
        Window window = mDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER; // 居中位置
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        submit_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.close();
            }
        });

        clear_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.jump();
            }
        });


        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append("在您使用微客货源前,请您无比审慎阅读,充分理解“用户协议”和”隐私政策“各条款,包括但不限于:为了向您提供即时通讯，内容分享等服务，我们需要您的手机设备信息，操作日志等个人信息。拧可以在“设置”中查看，变更，删除个人信息并管理您的授权,您可以阅读《用户协议》和《隐私政策》了解详细信息。如您同意，请点击同意并接受我们的服务");

        ClickableSpan cs1 = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                mCallBack.xiey(1);
            }
        };

        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#FFEC48"));
        spannableString.setSpan(colorSpan, 123, 129, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        spannableString.setSpan(cs1,123, 129, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        xieyi_tv.setMovementMethod(LinkMovementMethod.getInstance());
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(Color.parseColor("#464646"));
        spannableString.setSpan(colorSpan1, 130, 130, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        ClickableSpan cs2 = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                mCallBack.xiey(2);
            }
        };

        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(Color.parseColor("#FFEC48"));
        spannableString.setSpan(colorSpan2, 130, 136, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(cs2, 130, 136, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        xieyi_tv.setMovementMethod(LinkMovementMethod.getInstance());
        xieyi_tv.setText(spannableString);

    }
    public void close(){
        mDialog.dismiss();
    }

    public void show(){
        mDialog.show();
    }

    public void setCallBack(XieyiDialog.callBack callBack){
        this.mCallBack = callBack;
    }


    public  interface  callBack{
        void jump();
        void close();
        void xiey(int type);
    }

}
